import dva from 'dva';
import React from 'react';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import createLoading from 'dva-loading';
import Promise from 'promise-polyfill';
import 'babel-polyfill';
import { message } from 'antd';

import Login from './pages/login/Login';
import Root from './components/root/Root';

import './resources/iconfont/iconfont.less';
import './resources/styles/override-antd.less';
import './resources/styles/common.less';

if (!window.Promise) { // eslint-disable-line
  window.Promise = Promise; // eslint-disable-line
}


const models = [];
models.push(require('./components/root/model'));
models.push(require('./pages/login/model'));
models.push(require('./pages/order/model'));
models.push(require('./pages/settlement/model'));

const initialState = {};
models.forEach((m) => {
  initialState[m.namespace] = m.state;
});

// 1. Initialize
const ERROR_MSG_DURATION = 3; // 3 秒
const app = dva({
  onError(e) {
    message.error(e.message, ERROR_MSG_DURATION);
  },
});

// 2. Plugins
app.use(createLoading());

// 3. Model
models.forEach(item => app.model(item));

// 4. Router
app.router(() => {
  return (
    <Router>
      <Switch>
        <Route path="/login" component={Login} />
        <Route path="/" component={Root} />
      </Switch>
    </Router>
  );
});

// 5. Start
app.start('#root');
